package com.xuecheng.content.service.impl;

import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanWorkMapper;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanWork;
import com.xuecheng.content.service.TeachplanWorkService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanWorkServiceImpl implements TeachplanWorkService {

    @Resource
    private TeachplanMapper teachplanMapper;

    @Resource
    private TeachplanWorkMapper teachplanWorkMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindWork(TeachplanWork teachplanWork) {
        Long teachplanId = teachplanWork.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if (teachplan!=null)
        {
            teachplanWorkMapper.deleteByTeachplanId(teachplanId);
        }
        teachplanWork.setTeachplanId(teachplanId);
        teachplanWork.setCourseId(teachplan.getCourseId());
        teachplanWork.setCreateDate(LocalDateTime.now());
        teachplanWork.setChangeDate(LocalDateTime.now());
        teachplanWorkMapper.insert(teachplanWork);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unbindWork(Long teachplanWorkId) {
      teachplanWorkMapper.deleteById(teachplanWorkId);
    }

    @Override
    public TeachplanWork queryTeachplanWork(Long teachplanId) {
        return teachplanWorkMapper.selectByTeachplanId(teachplanId);
    }

    @Override
    public List<TeachplanWork> queryCourseWorks(Long courseId) {
        return teachplanWorkMapper.selectByCourseId(courseId);
    }

    @Override
    public List<TeachplanWork> queryByCourseId(Long courseId) {
        return teachplanWorkMapper.queryByCourseId(courseId);
    }
}